package edu.chl.da.ejb.ctrl;

import edu.chl.da.ejb.core.Account;
import edu.chl.da.ejb.core.Travelagent;
import edu.chl.da.ejb.persistency.ITravelagentFacade;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Max
 */
@Stateless
public class ManageAgent implements IManageAgent {

    @EJB
    ITravelagentFacade itf;

    @Override
    public Travelagent registerNewTravelagent(String username, String password, String email, String firstname, String lastname) {
        Travelagent t = new Travelagent();
        Account a = new Account();
        a.setUsername(username);
        a.setPassword(password);
        a.setEmail(email);
        a.setFirstname(firstname);
        a.setLastname(lastname);
        t.setAccount1(a);
        t.setAccount(username);
        a.setTravelagent(t);
        itf.create(t);
        return t;
    }

    @Override
    public Travelagent editTravelagent(Travelagent travelagent) {
        if(travelagent == null){
            throw new NullPointerException("Travelagent is null");
        }
        itf.edit(travelagent);
        return travelagent;
    }

    @Override
    public boolean removeTravelagent(Travelagent travelagent) {
        if(travelagent != null){
            itf.remove(travelagent);
            return true;
        }
        else{
            throw new NullPointerException("Travel agent is null.");
        }
    }

    @Override
    public Travelagent getTravelagent(String username) {
        return itf.find(username);
    }

    @Override
    public List<Travelagent> getTravelagentList() {
        return itf.findAll();
    }
}
